package com.sjksy.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjksy.demo.domain.RouteSpots;
import com.sjksy.demo.domain.Spots;
import com.sjksy.demo.domain.TravelRoute;
import com.sjksy.demo.dto.travel_route.TravelRouteRequestDto;
import com.sjksy.demo.dto.travel_route.TravelRouteResponseDto;
import com.sjksy.demo.mapper.RouteSpotsMapper;
import com.sjksy.demo.mapper.SpotsMapper;
import com.sjksy.demo.msmapper.travel_route.TravelRouteMsMapper;
import com.sjksy.demo.service.TravelRouteService;
import com.sjksy.demo.mapper.TravelRouteMapper;
import com.sjksy.demo.vo.travel_route.TravelRouteRequestVo;
import com.sjksy.demo.vo.travel_route.TravelRouteResponseVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author guyueyuan
* @description 针对表【travel_route】的数据库操作Service实现
* @createDate 2024-12-11 15:37:03
*/
@Service
public class TravelRouteServiceImpl extends ServiceImpl<TravelRouteMapper, TravelRoute>
    implements TravelRouteService{

    @Resource
//    @Autowired
    TravelRouteMapper travelRouteMapper;
    @Resource
    RouteSpotsMapper routeSpotsMapper;
    @Resource
    SpotsMapper spotsMapper;
    @Resource
    TravelRouteMsMapper travelRouteMsMapper;

    @Override
    public TravelRouteResponseVo getTravelRouteById(Integer id) {
//        其实这种多表联查使用自己写sql似乎更快

//        先查到路线信息
        TravelRoute travelRoute = travelRouteMapper.selectById(id);
//        条件筛寻到线路与景点映射表
        QueryWrapper<RouteSpots> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("route_id",id);
        List<RouteSpots> routeSpots = routeSpotsMapper.selectList(queryWrapper1);
//        这个新语法可以多理解一下
        List<Integer> spotIds = routeSpots.stream()
                .map(RouteSpots::getSpotId)
                .distinct()
                .collect(Collectors.toList());
//        条件筛选到景点数据
        QueryWrapper<Spots> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.in("spot_id",spotIds);
        List<Spots> spots=spotsMapper.selectList(queryWrapper2);
//      组合转换数据类型
        TravelRouteResponseDto travelRouteResponseDto=travelRouteMsMapper.entityToTravelRouteDto(travelRoute);
        travelRouteResponseDto.setSpots(spots);

        return travelRouteMsMapper.dtoToTravelRouteResponseVo(travelRouteResponseDto);
    }

    @Override
    @Transactional
    public boolean postTravelRoute(TravelRouteRequestVo travelRouteRequestVo) {
//        先转换对象
        TravelRouteRequestDto travelRouteRequestDto = travelRouteMsMapper.voToTravelRouteRequestDto(travelRouteRequestVo);
        List<Integer> spotsIds = travelRouteRequestDto.getSpotsIds();
        TravelRoute travelRoute=travelRouteMsMapper.dtoToTravelRoute(travelRouteRequestDto);

//        Long generatedId = travelRoute.getId(); // 获取插入后的主键
//        插入成功后，MyBatis-Plus 会自动将生成的主键值设置到实体对象中
//        System.out.println(travelRoute);
        int insert = travelRouteMapper.insert(travelRoute);
        if (insert<=0){
            return false;
        }
        int routeId=travelRoute.getRouteId();

//        插入线路景点关联数据
        spotsIds.forEach((spotId)->{
            RouteSpots routeSpots = new RouteSpots();
            routeSpots.setRouteId(routeId);
            routeSpots.setSpotId(spotId);
            routeSpotsMapper.insert(routeSpots);
        });
        return true;
    }

    @Override
    public List<TravelRouteResponseVo> getAllSpots() {
//        先把所有查出来
        List<TravelRoute> travelRoutes = travelRouteMapper.selectList(null);
//        调用这个service层其它方法，能复用就复用
        List<TravelRouteResponseVo> travelRouteResponseVoList=new ArrayList<>();
        travelRoutes.forEach((travelRoute -> {
            travelRouteResponseVoList.add(getTravelRouteById(travelRoute.getRouteId()));
        }));
//        返回总的数据（这个可能其实会比较多）
        return travelRouteResponseVoList;
    }
}




